#ifndef PCA21125_H_
#define PCA21125_H_

/*==================================================================================================
*                                         INCLUDE FILES
==================================================================================================*/
//#include "typedefs.h"
#include "Std_Types.h"
#include "Spi.h"

#define  ERROR_NOT_OK               E_NOT_OK
#define  ERROR_OK                   E_OK
#define  INIT_En                    0u
#define  INIT_SET_Time              1u
#define  PCA21125_InitErr_Num       0u
#define  PCA21125_MAX_INITERR_NUM   20u
#define  Control1_register_address  0x00
#define  Control2_register_address  0x01
#define  Seconds_register_address   0x02
#define  Minutes_register_address   0x03
#define  Hours_register_address     0x04
#define  Days_register_address      0x05
#define  Weekdays_register_address  0x06
#define  Months_register_address    0x07
#define  Years_register_address     0x08
#define  Min_alarm_address          0x09
#define  Hour_alarm_address         0x0A
#define  Day_alarm_address          0x0B
#define  Weekday_alarm_address      0x0C
#define  Set_Timer_Step             0u
#define  Set_TimerStep_Err          0u
#define  Set_TimerStep_MaxErrNum    20u
#define  write_command              1
#define  read_command               9




/*spi parameter definition*/
#define PCA21125_CHANNEL                SpiConf_SpiSequence_SpiSequence_PCA21125
#define PCA21125_SPIMAX_EBBUFF          32u
#define PCA21125_SPIMAX_DELAY_TIME      5u 




typedef struct
{
	union{
		unsigned char R;
		struct{
			uint8_t EXT_TEST:1;
			uint8_t  N1:1;
			uint8_t  STOP:1;
			uint8_t  N2:1;
			uint8_t  POR_OVRD:1;
			uint8_t  twelve_twentyfour:1;
			uint8_t  N3:2;
		}B;
	}Control_1;//00h
	
	union{
		unsigned char R;
		struct{
			uint8_t  MI:1;
			uint8_t  SI:1;
			uint8_t  MSF:1;
			uint8_t  TI_IP:1;
			uint8_t  AF:1;
			uint8_t  TF:1;
			uint8_t  AIE:1;
			uint8_t  TIE:1;			
		}B;
	}Control_2;//01h
	
	union{
		unsigned  char R;
		struct{
			unsigned char RF:1;
			unsigned char TEN:3;
			unsigned char UNIT:4;			
		}B;
	}Seconds;//02h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:1;
			unsigned char TEN:3;
			unsigned char UNIT:4;			
		}B;
	}Minutes;//03h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:2;			
			unsigned char AMPM:1;
			unsigned char TEN:1;
			unsigned char UINT:4;
		}B;
	}Hours_12;//04h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:2;			
			unsigned char TEN:2;
			unsigned char UINT:4;
		}B;
	}Hours_24;//04h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:2;	
			unsigned char TEN:2;
			unsigned char UINT:4;			
		}B;
	}Days;//05h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:5;			
			unsigned char WEEKDAYS:3;			
		}B;
	}Weekdays;//06h
	
	union{
		unsigned  char R;
		struct{
			unsigned char N:3;	
			unsigned char TEN:1;
			unsigned char UINT:4;			
		}B;
	}Months;//07h
	
	union{
		unsigned  char R;
		struct{
			unsigned char TEN:4;			
			unsigned char UINT:4;			
		}B;
	}Years;//08h
	
	union{
		unsigned  char R;
		struct{
			unsigned char AE_M:1;			
			unsigned char TEN:3;
			unsigned char UINT:4;
		}B;
	}Minute_alarm;//09h
	
	union{
		unsigned  char R;
		struct{
			unsigned char AE_H:1;
			unsigned char N:1;
			unsigned char AMPM:1;
			unsigned char TEN:1;
			unsigned char UINT:4;
		}B;
	}Hour_12_alarm;//10h

	union{
		unsigned  char R;
		struct{
			unsigned char AE_H:1;
			unsigned char N:1;
			unsigned char TEN:2;
			unsigned char UINT:4;
		}B;
	}Hour_24_alarm;//0Ah
	
	union{
		unsigned  char R;
		struct{
			unsigned char AE_D:1;
			unsigned char N:1;
			unsigned char TEN:2;
			unsigned char UINT:4;
		}B;
	}Day_alarm;//0Bh
	
	union{
		unsigned  char R;
		struct{
			unsigned char AE_W:1;
			unsigned char N:4;
			unsigned char UINT:3;
		}B;
	}Weekday_alarm;//0Ch
		
	union{
		unsigned  char R;
		struct{
			unsigned char N:5;
			unsigned char COF:3;
		}B;
	}CLKOUT_control;//ODh
	
	union{
		unsigned  char R;
		struct{
			unsigned char TE:1;
			unsigned char N:5;
			unsigned char COF:2;
		}B;
	}Timer_control;//0Eh
	
	union{
		unsigned  char R;
		struct{
			unsigned char T;
		}B;
	}Countdown_timer;//0Fh
}PCA21125_Regs,*pPCA21125_Regs;

typedef struct
{
	 union{
		unsigned  char R;
		struct{
			unsigned char  RF:1;
			unsigned char  TEN:3;
			unsigned char  UINT:4;	
			
		}B;
	}Seconds;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  N3:1;
			unsigned char  TEN:3;
			unsigned char  UINT:4;				
		}B;
	}Minutes;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  N3:2;			
			unsigned char AMPM:1;
			unsigned char  TEN:1;
			unsigned char  UINT:4;			
		}B;
	}Hours_12;

	union{
		unsigned  char R;
		struct{
			unsigned char  N3:2;			
			unsigned char  TEN:2;
			unsigned char  UINT:4;			
		}B;
	}Hours_24;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  N3:2;
			unsigned char  TEN:2;
			unsigned char  UINT:4;			
		}B;
	}Days;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  N3:5;			
			unsigned char  WEEKDAYS:3;			
		}B;
	}Weekdays;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  N3:3;			
			unsigned char  TEN:1;
			unsigned char  UINT:4;
		}B;
	}Months;
	
	union{
		unsigned  char R;
		struct{
			unsigned char  TEN:4;			
			unsigned char  UINT:4;			
		}B;
	}Years;
}BCD_Time,*pBCD_Time;

typedef struct {
	unsigned char  Seconds;
	unsigned char  Minutes;
	unsigned char  Hours;
	unsigned char  Days;
	unsigned char  Weekdays;
	unsigned char  Months;
	unsigned char  Years;
}Time,*pTime;

typedef union{
	unsigned char R;
	struct {
		unsigned char RW:1;	//data read or data write selection;
							//0 is write data;1 is read data;
		unsigned char SA:3; //sub address;other codes cause the device to ignore data transfer;
		unsigned char RA:4; //register address
	}B;
}Command,*pCommand;

typedef enum {
	WRITE_CMD,
	READ_CMD
}CMD;

typedef enum {
	WRITE_TIME,
	READ_TIME,
}CMD_FLAG;

extern BCD_Time BCD_time;
extern PCA21125_Regs PCA21125_regs;

//extern uint8_t PCA21125_Settime(uint8_t init_year, uint8_t init_month, uint8_t init_weekdays, uint8_t init_days, uint8_t init_hours, uint8_t init_mins, uint8_t init_seconds); 
extern uint8_t PCA21125_Settime(uint32_t timevlue);
extern uint8_t PCA21125_ClearRF(void);
extern uint8_t PCA21125_EnableAlarm(void);
extern uint8_t PCA21125_SetAlarm(uint8_t min_data, uint8_t hour_data, uint8_t day_data, uint8_t weekday_data, uint8_t ae_m, uint8_t ae_h, uint8_t ae_d, uint8_t ae_w); 
extern unsigned char DecToBCD(unsigned char dec);
extern void PCA21125_Init(void);
extern uint8_t PCA21125_Write_Inittime(uint8_t init_year, uint8_t init_month, uint8_t init_weekdays, uint8_t init_days, uint8_t init_hours, uint8_t init_mins, uint8_t init_seconds);
extern uint8_t PCA21125_ClearAF(void);
extern uint8_t PCA21125_ClearTF(void);
extern uint8_t PCA21125_DisableCountdown(void);
extern void RTC_Management(void);




#endif /* PCA21125_H_ */

